~(function() {

	var PromotionIndexScroll = {
		create : function() {
			return this;
		},
		initialize : function(element, height) {
			this.stop();
			this.element = document.getElementById(element);

			var divContainer = this.element.getElementsByTagName("div");
			if (divContainer.length > 0) {
				var divs = divContainer[0].getElementsByTagName("div");
				if (divs.length > 0) {
					var extendDivInnerHTML = divs[0].innerHTML;
					divContainer[0].innerHTML = divContainer[0].innerHTML + "<div>" + extendDivInnerHTML + "</div>";
				}
			}

			this.stepHeight = height;
			this.maxHeight = this.element.scrollHeight - this.stepHeight;

			this.scrolled = 0;
			this.counter = 0;

			this.scroll();

			this.timer = undefined;
			this.element.onmouseover = this.stop.bind(this);
			this.element.onmouseout = function() {
				this.timer = setTimeout(this.scroll.bind(this), 1000);
			}.bind(this);
		},
		scroll : function() {
			if (this.scrolled < this.maxHeight) {
				this.scrolled = this.scrolled + 1;
				this.element.scrollTop = this.scrolled;
				this.counter = this.counter + 1;
			} else {
				this.scrolled = 0;
				this.element.scrollTop = this.scrolled;
				this.counter = 0;
			}

			if (this.counter < this.stepHeight) {
				this.timer = setTimeout(this.scroll.bind(this), 30);
			} else {
				this.counter = 0;
				this.timer = setTimeout(this.scroll.bind(this), 3000);
			}
		},
		stop : function() {
			clearTimeout(this.timer);
		}
	}

	/**
	 * var data = [[${promotions}]]; data= [{ templetUrl:'http://www.baidu.com', templetDesc:'百度', title:'百度搜索' },{ templetUrl:'http://www.google.hk', templetDesc:'Google', title:'Google搜索' }]; if(data.length > 0){ var innerHTML = ''; for(var i in data){ var promotion = data[i]; var promotionHTML = "<div><img src=\"" + [[@{'/img/news.png'}]] + "\"/><div class='div_a'><a href=\"javascript:openTabs('" + promotion.templetUrl + "','" + promotion.templetDesc + "')\">" + promotion.title + "</a></div></div>";
	 * innerHTML += promotionHTML; } document.getElementById("promotion_index").getElementsByTagName("div")[0].innerHTML = innerHTML; document.getElementById("promotion_index").style.height = '26px';
	 * 
	 * if(data.length > 1){ var scrollPromotion = PromotionIndexScroll.create(); scrollPromotion.initialize("promotion_index", 26); } }
	 */

	// 查询广告
	var p = {};
	p.position = "BMS_INDEX";
	$.ajax({
		url : _PROMOTION_URL,
		cache : false,
		data : p,
		dataType : 'json',
		success : function(data) {
			if (data && data.length > 0) {
				var innerHTML = '';
				for ( var i in data) {
					var promotion = data[i];
					innerHTML += "<div><img src=\"" + _PROMOTION_IMG_URL + "\"/><div class='div_a'><a href=\"javascript:promitionOpenTabs('" + promotion.templetUrl + "','" + promotion.templetDesc + "')\">" + promotion.title + "</a></div></div>";
				}
				document.getElementById("promotion_index").getElementsByTagName("div")[0].innerHTML = innerHTML;
				$("#promotion_index").animate({
					height : '26px'
				}, 1000, function() {
					var scrollPromotion = PromotionIndexScroll.create();
					scrollPromotion.initialize("promotion_index", 26);
				});
			}
		}
	});
})(jQuery);

function promitionOpenTabs(url, title) {
	var a = document.createElement('a');
	a.href = url;

	if (a.hostname == location.hostname) {
		addTabs(url, title);
	} else {
		window.open(url);
	}
}